// JavaScript Document
//TimeModule.js
function TimeModule(Configurations)//主函数
{
	var Rows=7;//七列
	var Lines=6;//六行，可能变化
	var DaysInMonth=31;//当前月天数
	var DateNow=new Date();
	var DateYear=DateNow.getFullYear();//获取完整的年份(4位,1970-????)
	var DateMonth=DateNow.getMonth()+1;//因为返回冲0开始
	var DateDay=DateNow.getDate();//获取当前天(这里指的是某月某号中的号数),即当前被选中的天数;
	var DateDayOfWeek=DateNow.getDay();
	var DateDaysOfWeek=['周日','周一','周二','周三','周四','周五','周六'];
	var DateDaysOfWeekInShort=['日','一','二','三','四','五','六'];
	var BottomButton=['取消','确定'];
	var TimeModuleMonthsInShort=['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'];
	var WeekBeginDay=7;
	var FirstDayInWeek=DateNow.getDay();//当前天对应的星期
	var BeginFillDay=0;//填充时的起始填充位置;
	var DateHour="00";
	var DateMinute="00";
	var InputElement=$(this);//因为主要配合Input使用
	var IfFirstAppear=true;//判断是否第一次出现，若是那么就有个更长的动画效果
	var DisplayZone="Down";//显示在什么位置，上方下方还是屏幕中央
	var IfBackground=false;//是否有背景
	var BackgroundColor="white";
	var MainColor="RGB(0,150,136)";
	var Color="black";
	var FontFamily="微软雅黑";
	var ChooseBarDisplay="Days";
	var TimeModuleWidth="550px";
	var TimeModuleLeft=null;
	var TimeModuleTop=null;
	var TimeModulePosition="absolute";
	var ClockBackgroundColor="rgb(237,237,237)";
	var ClockColor="";
	var TimeModuleStyle="auto";//横着或者水平,自动调节
	var TimeModuleStyleNow="";
	var TimeModuleClockBase=24;
	var TimeModuleClockCycle=2;
	var TimeModuleAutoJump="auto";
	var TimeModuleBottomButton=true;
	var TimeModuleBottomButtonBottomPadding=10;
	var TimeModuleBottomButtonHeight=20;
	RemoveTimeModule();//移除整个模块
	AnalyseConfiguration(Configurations);
	ShowTimeModule();
	return;
	function AnalyseConfiguration(Configurations)//解析配置信息
	{
		var Keys=Array();
		var KeysValue=Array();
		for (var key in Configurations)
		{
			Keys.push(key.toLowerCase());
			KeysValue.push(Configurations[key]);	
		}
		var Length=Keys.length;
		for (var i=0;i<Length;++i)
		{
			switch (Keys[i])
			{
				case "width":
					TimeModuleWidth=KeysValue[i];
					break;
				case "left":
					TimeModuleLeft=KeysValue[i];
					break;
				case "top":
					TimeModuleTop=KeysValue[i];
					break;
				case "font-family":
					FontFamily=KeysValue[i]+","+FontFamily;
					break;
				case "color":
					Color=KeysValue[i];
					break;
				case "background-color":
					BackgroundColor=KeysValue[i];
					break;
				case "main-color":
					MainColor=KeysValue[i];
					break;
				case "clock-background-color":
					ClockBackgroundColor=KeysValue[i];
					break;
				case "clock-color":
					ClockColor=KeysValue[i];
					break;
				case "clock-base":
					if (Number(KeysValue[i])==12)
					{
						TimeModuleClockBase=Number(KeysValue[i]);
						TimeModuleClockCycle=1;
					}
					break;
				case "language":
					if (KeysValue[i].toLowerCase()=="english")
					{
						DateDaysOfWeekInShort=['S','M','T','W','T','F','S'];
						DateDaysOfWeek=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
						BottomButton=['CANCEL','OK'];
						TimeModuleMonthsInShort=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec'];
					}
					break;
				case "week-begin":
					if ((KeysValue[i]>=1)&&(KeysValue[i]<=7))
						WeekBeginDay=KeysValue[i];
					break;
				case "style":
					if ((KeysValue[i].toLowerCase()=="vertical")||(KeysValue[i].toLowerCase()=="horizontal"))
						TimeModuleStyle=KeysValue[i].toLowerCase();
					break;
				case "auto-jump":
					if ((KeysValue[i]==true)||(KeysValue[i]==false))
						TimeModuleAutoJump=KeysValue[i];
					break;
				case "bottom-button":
					if (KeysValue[i]==false)
					{
						TimeModuleBottomButton=KeysValue[i];
						TimeModuleBottomButtonHeight=0;
						TimeModuleAutoJump=true;
					}
					break;
				case "position":
					if ((KeysValue[i].toLowerCase()=="relative")||(KeysValue[i].toLowerCase()=="fixed"))
						TimeModulePosition=KeysValue[i].toLowerCase();
					break;
				case "input":
					InputElement=KeysValue[i];
					break;
				default:
					break;
			}
		}
		ClockColor=(ClockColor=="")?Color:ClockColor;
		return;	
	}
	function CalculateParameter()//计算一系列参数
	{
		DateNow.setYear(DateYear);//设置年份
		DateNow.setMonth(DateMonth-1);//因为Month从0开始
		DateNow.setDate(1);//设置为该月1号
		//先判断当前月有多少天，这里要考虑二月份
		if ((DateYear%4)==0)
			var Months=[31,29,31,30,31,30,31,31,30,31,30,31];
		else
			var Months=[31,28,31,30,31,30,31,31,30,31,30,31];
		DaysInMonth=Months[DateMonth-1];
		FirstDayInWeek=DateNow.getDay();//虽然返回0-6，但是这里0代表周日，因此不需要加1
		Lines=parseInt((DaysInMonth+FirstDayInWeek)/7);
		if (parseInt(DaysInMonth+FirstDayInWeek)%7!=0)
		{
			Lines=Lines+1;
		}
		BeginFillDay=(FirstDayInWeek+7-WeekBeginDay)%7;
	}
	function FillTimeModule()
	{
		//先改变最上方所显示的当前所选择年月
		var ChooseYearAndMonth=DateYear+" "+DateMonth;
		$("#ChooseYearAndMonth").html(ChooseYearAndMonth);
		$("td.FillDays div").html("");//先初始化
		for (var i=1;i<=DaysInMonth;++BeginFillDay,++i)
		{
			var FillDay="td.FillDays:eq("+BeginFillDay+") div";
			$(FillDay).html(i);	
		}
	}
	function VerticalTimeModule()
	{
		TimeModuleStyleNow="vertical";
		var Content='<tr>';	
		Content+='<td id="TimeModuleDisplayBar" style="color:white;background-color:'+MainColor+';height:80px;;padding-top:20px;padding-left:10px" valign="top"></td></tr>';//构建LeftTimeBar
		Content+='<tr><td id="TimeModuleChooseBar"></td></tr>';
		return Content;
	}
	function HorizontalTimeModule()
	{
		TimeModuleStyleNow="horizontal";
		var Content='<tr>';	
		Content+='<td id="TimeModuleDisplayBar" style="color:white;background-color:'+MainColor+';width:30%;padding-top:20px;padding-left:10px" valign="top"></td>';//构建LeftTimeBar
		Content+='<td id="TimeModuleChooseBar" style="width:70%"></td></tr>';
		return Content;
	}
	function AppendTimeModule()//添加TimeModule
	{
		var TimeModule='<div id="TimeModule" style="-webkit-user-select:none;width:'+TimeModuleWidth+';display:none;z-index:105;position:'+TimeModulePosition+';border:1px solid #9e9e9e;box-shadow:2px 2px 1px #9e9e9e;font-family:'+FontFamily+';color:'+Color+';background-color:'+BackgroundColor+';">';//首先用一个大div包裹
		TimeModule+='<table border="0" cellspacing="0" cellpadding="0" style="width:100%">';//借助table实现左右栏
		if (TimeModuleStyle=="vertical")
		{
			TimeModule+=VerticalTimeModule();
		}
		else if (TimeModuleStyle=="horizontal")
		{
			TimeModule+=HorizontalTimeModule();
		}
		else
		{
			var WindowWidth=$(window).width();
			var WindowHeight=$(window).height();
			if (WindowWidth<WindowHeight)
				TimeModule+=VerticalTimeModule();
			else
				TimeModule+=HorizontalTimeModule();	
		}
		//首先是顶部显示时间的栏，称为LeftTimeBar，与MainTheme结合组成不同风格
		TimeModule=TimeModule+'</table>';
		$(document.body).append(TimeModule);
		AdjustWidth();
		AppendDisplayBar();
		AppendChooseBar();
	}
	function AppendDisplayBar()
	{
		if (ChooseBarDisplay=="Days")
		{
			AppendDisplayBarDays();
		}
		else
		{
			AppendDisplayBarTime();
		}
	}
	function AppendDisplayBarTime()
	{
		var Content,Top;	
		if (TimeModuleStyleNow=="vertical")
		{
			Content='<div id="DisplayTimeBarTime" style="margin:0px auto;text-align:center;font-size:48px;position:relative">'+Number(DateHour)+':'+Number(DateMinute)+'</div>';	
			Top=0;
		}
		else
		{
			Content='<div id="DisplayTimeBarTime" style="margin:0px auto;text-align:center;font-size:48px;position:relative">'+Number(DateHour)+':'+Number(DateMinute)+'</div>';	
			Top=($("#TimeModuleDisplayBar").height()-$("#DisplayTimeBarTime").height())/2-45;
		}
		$("#TimeModuleDisplayBar").html(Content);
		$("#DisplayTimeBarTime").css({"top":Top});
	}
	function AdjustDisplayBarTime()
	{
		$("#DisplayTimeBarTime").html(Number(DateHour)+':'+Number(DateMinute));
	}
	function AppendDisplayBarDays()
	{
		var Content='<div>';
		Content+='<div id="DisplayTimeBarYear" style="font-size:16px;position:relative;">'+DateYear+'</div>';//显示年份;
		Content+='<div><span id="DisplayTimeBarDayOfWeek" style="height:40px;line-height:40px;font-size:30px;position:relative">'+DateDaysOfWeek[DateDayOfWeek]+', '+'</span>';//显示星期几
		if (TimeModuleStyleNow=="horizontal")
			Content+='<br/>';
		Content+='<span id="DisplayTimeBarDay" style="height:40px;line-height:40px;font-size:30px;position:relative;">'+TimeModuleMonthsInShort[DateMonth-1]+' '+DateDay+'</span></div>';//显示月日div
		Content+='</div>';	
		$("#TimeModuleDisplayBar").html(Content);		
	}
	function AppendChooseBar()
	{
		if (ChooseBarDisplay=="Days")
		{
			AppendChooseBarDays();
		}
		else if (ChooseBarDisplay=="Hours")
		{
			AppendChooseBarHours();
		}
		else
			;
	}
	function AppendChooseBarHours()
	{
		var ClockSize,ClockMarginLeft,ClockMarginTop,ClockRadius;
		if (TimeModuleStyleNow=="vertical")
		{
			ClockSize=($("#TimeModule").width()-20)*0.8;
			ClockMarginLeft=($("#TimeModule").width()-ClockSize)/2;
			ClockMarginTop=($("#TimeModule").height()-TimeModuleBottomButtonBottomPadding-TimeModuleBottomButtonHeight)*0.1;
			TimeModuleBottomButtonBottomPadding
			
			ClockRadius=ClockSize/2;		
		}
		else
		{
			ClockSize=($("#TimeModule").height()-TimeModuleBottomButtonBottomPadding-TimeModuleBottomButtonHeight)*0.8;
			ClockMarginLeft=($("#TimeModule").width()*0.7-ClockSize)/2;
			ClockMarginTop=($("#TimeModule").height()-TimeModuleBottomButtonBottomPadding-TimeModuleBottomButtonHeight)*0.1;
			ClockRadius=ClockSize/2;	
		}
		var Content='<div id="TimeModuleClock" style="border:none;background-color:'+ClockBackgroundColor+';position:relative;margin:'+ClockMarginTop+'px '+ClockMarginLeft+'px;width:'+ClockSize+'px;height:'+ClockSize+'px;border-radius:'+ClockRadius+'px;color:'+ClockColor+';"></div>';
		Content+='<div class="OperateArea" style="padding-bottom:'+TimeModuleBottomButtonBottomPadding+'px;height:+'+TimeModuleBottomButtonHeight+'px;font-size:16px;color:'+MainColor+'">';
		if (TimeModuleBottomButton)
		{
			var FirstButtonLeft,SecondButtonLeft;
			if (TimeModuleStyleNow=="vertical")
			{
				FirstButtonLeft="50%";
				SecondButtonLeft="82%";	
			}
			else
			{
				FirstButtonLeft="60%";
				SecondButtonLeft="82%";	
			}
			Content+='<div class="CancelChooseTime" style="cursor:pointer;position:relative;width:50px;height:20px;left:'+FirstButtonLeft+';">'+BottomButton[0]+'</div>';
			Content+='<div class="ConfirmChooseTime" style="cursor:pointer;position:relative;width:50px;height:20px;left:'+SecondButtonLeft+';top:-20px">'+BottomButton[1]+'</div>';
		}
		Content+='</div>';
		$("#TimeModuleChooseBar").html(Content);
		
		$("div.CancelChooseTime").click(function (){RemoveTimeModule();});
		$("div.ConfirmChooseTime").one("click",function (e){setTimeout(function(){AppendHoursAndMinutesDiv("Minute",60,1,5,true);},300);});
		AppendHoursAndMinutesDiv("Hour",TimeModuleClockBase,TimeModuleClockCycle,1,true);
	}
	function AppendHoursAndMinutesDiv(TimeType,Numbers,Cycle,NumbersInterval,BiggestToZero)//放多少个数字，圈数
	{
		$("#TimeModuleClock").html("");
		if (Cycle==null)
			Cycle=1;
		if (NumbersInterval==null)
			NumbersInterval=1;
		if (BiggestToZero==null)
			BiggestToZero=false;
		var ClockSize=$("#TimeModuleClock").width();
		var ClockCenter=ClockSize/2;
		var DivSize=ClockSize*0.15;
		var DivRadius=DivSize/2;
		if (Cycle==1)
			var Length=ClockSize*0.4;
		else
			var Length=ClockSize*0.28;
		var IntervalAngle=(360/Numbers)*Cycle;
		var DivClass=(TimeType=="Hour")?"TimeModuleClockHourDiv":"TimeModuleClockMinuteDiv";
		for (var i=1;i<=Numbers;++i)
		{
			var DivLeft=ClockCenter+Number(Length*Math.sin(i*IntervalAngle*Math.PI/180))-DivSize/2;	
			var DivTop=ClockCenter-Length*Math.cos(i*IntervalAngle*Math.PI/180)-DivSize/2;
			var DivPadding=(DivSize-16)/2;
			var DivContent=(i%NumbersInterval==0)?((i==Numbers)&&(BiggestToZero))?"00":i:"";
			var DivValue=((i==Numbers)&&(BiggestToZero))?"00":i;
			var Div='<div class="TimeModuleClockDiv '+DivClass+'" style="cursor:pointer;text-align:center;font-size:16px;z-index:120;position:absolute;top:'+DivTop+'px;left:'+DivLeft+'px;width:'+DivSize+'px;height:'+DivSize+'px;border-radius:'+DivRadius+'px;line-height:'+DivSize+'px;" value='+DivValue+'>'+DivContent+'</div>';	
			$("#TimeModuleClock").append(Div);
			if ((Cycle==2)&&(i==(Numbers/Cycle)))
			{
				Length=ClockSize*0.4;
			}
		}
		//添加中心点
		var CenterDivLeft=ClockCenter-4;
		var CenterDivTop=ClockCenter-4;
		var CenterDiv='<div id="TimeModuleClockCenter" style="position:absolute;width:8px;height:8px;border-radius:4px;background-color:'+MainColor+';left:'+CenterDivLeft+'px;top:'+CenterDivTop+'px;"></div>';
		$("#TimeModuleClock").append(CenterDiv);
		//添加条
		var LineDivLeft=ClockCenter;
		var LineDivTop=ClockCenter-1;
		var LineDivWidth=Length-DivSize/2+5;
		/*要稍微长一些*/
		var LineDiv='<div id="TimeModuleClockLine" style="position:absolute;height:2px;background-color:'+MainColor+';width:'+LineDivWidth+'px;left:'+LineDivLeft+'px;top:'+LineDivTop+'px;"></div>';
		$("#TimeModuleClock").append(LineDiv);
		$("div.TimeModuleClockDiv").click(function (){CalculateClock(event.pageX,event.pageY,TimeType,Numbers,Cycle);});
		$("div.TimeModuleClockHourDiv").click(function (e){if (TimeModuleAutoJump!=false)
															setTimeout(function(){AppendHoursAndMinutesDiv("Minute",60,1,5,true);},300);});
		$("div.TimeModuleClockDiv").mousedown(function(event){RotateAndChange(event,TimeType,Numbers,Cycle);});
		$("div.TimeModuleClockDiv").bind("touchstart",function(event){event.preventDefault();RotateAndChange(event,TimeType,Numbers,Cycle);});
		PaintClock(TimeType,Numbers,Cycle,1);
		if (TimeType=="Minute")
			$("div.ConfirmChooseTime").one("click",function (e){FillInputElement();HideTimeModule();});
	}
	function CalculateClock(X,Y,TimeType,Numbers,Cycle)
	{
		var CenterX=$("#TimeModuleClockCenter").offset().left+$("#TimeModuleClockCenter").width()/2;
		var CenterY=$("#TimeModuleClockCenter").offset().top-$("#TimeModuleClockCenter").width()/2;
		var ClockRadius=$("#TimeModuleClock").width()/2;
		var LineLength=$("#TimeModuleClockLine").width();
		var dy=Y-CenterY;
		var dx=X-CenterX;
		var dl=Math.pow((Math.pow(dx,2)+Math.pow(dy,2)),0.5);
		var Angle=Math.atan(dy/dx)*180/Math.PI;
		Angle+=90;
		if (dx<0)
			Angle+=180;
		
		if (TimeType=="Hour")
		{
			DateHour=((Angle)*Numbers/Cycle)/360;
			if ((Cycle==2))
			{
				if (dl<LineLength)
					DateHour=Math.round(DateHour);
				else
					DateHour=Math.round(DateHour)+Numbers/Cycle;
				if (DateHour==0)
					DateHour=Numbers/Cycle;
			}
			else
			{
				DateHour=Math.round(DateHour);
				if (DateHour==0)
					DateHour=Numbers/Cycle;
			}
			SetDateNow();
			PaintClock("Hour",Numbers,Cycle,1);	
		}
		else
		{
			DateMinute=(Angle)/6;
			DateMinute=Math.round(DateMinute);
			SetDateNow();
			PaintClock("Minute",60,1,5);
		}
	}
	function RotateAndChange(event,TimeType,Numbers,Cycle)
	{
		var End=0;
		$("div.TimeModuleClockDiv").click(function(event){End=1;CalculateClock(event.pageX,event.pageY,TimeType,Numbers,Cycle);
															if ((TimeType=="Hour")&&(TimeModuleAutoJump!=false))
																setTimeout(function(){AppendHoursAndMinutesDiv("Minute",60,1,5,true);},300);
															else if ((TimeType=="Minute")&&(TimeModuleAutoJump!=false))
															{
																setTimeout(function (){FillInputElement();RemoveTimeModule();},300);	
															}
															});
		if (event.type!="touchstart")
			$("#TimeModuleChooseBar").mousemove(RotateDiv);	
		else
		{
			$("#TimeModuleChooseBar")[0].addEventListener('touchmove',RotateDiv,false);//添加监听事件
		}
		function RotateDiv(event)
		{
			$("#TimeModuleChooseBar").bind("touchcancel touchend mouseup",function(event){if (End==1)
															return;
														End=1;
														if (TimeModuleAutoJump==true)
														{
															CalculateClock(event.pageX,event.pageY,TimeType,Numbers,Cycle);
															if (TimeType=="Hour")
																setTimeout(function(){AppendHoursAndMinutesDiv("Minute",60,1,5,true);},300);
															else if ((TimeType=="Minute")&&(TimeModuleAutoJump==true))
															{
																setTimeout(function (){FillInputElement();RemoveTimeModule();},300);	
															}
														}});
			if (End==1)
				return;
			else
			{
				if (event.type!="touchmove")
					CalculateClock(event.pageX,event.pageY,TimeType,Numbers,Cycle);
				else
				{
					CalculateClock(event.touches[0].pageX,event.touches[0].pageY,TimeType,Numbers,Cycle);
				}
			}
		}
	}
	function PaintClock(TimeType,Numbers,Cycle,NumbersInterval)
	{
		if (TimeType=="Hour")
			var DivNumber=(Number(DateHour)==0)?Numbers-1:Number(DateHour)-1;
		else
			var DivNumber=(Number(DateMinute)==0)?Numbers-1:Number(DateMinute)-1;
		for (var i=0;i<Numbers;++i)
		{
			var Div=$('div.TimeModuleClockDiv:eq('+i+')');
			if (Div.html()=="")
				Div.css({"background-color":"inherit","z-index":"120"});
			else
				Div.css({"background-color":"inherit","color":ClockColor,"z-index":"121"});
		}
		$('div.TimeModuleClockDiv:eq('+DivNumber+')').css({"background-color":MainColor,"color":"white","z-index":"125"});
		var IntervalAngle=(360/Numbers)*Cycle;
		TimeModuleRotate(IntervalAngle*(DivNumber+1)-90);
		if (DivNumber>=Numbers/Cycle)
			$("#TimeModuleClockLine").css({"z-index":"121"});
		else
			$("#TimeModuleClockLine").css({"z-index":"120"});
		AdjustDisplayBarTime();
	}
	function TimeModuleRotate(value)
	{
		document.getElementById('TimeModuleClockLine').style.WebkitTransformOrigin ="0 50%";
		document.getElementById('TimeModuleClockLine').style.webkitTransform="rotate(" + value + "deg)";
		document.getElementById('TimeModuleClockLine').style.MozTransform="rotate(" + value + "deg)";
		document.getElementById('TimeModuleClockLine').style.OTransform="trotate(" + value + "deg)";
		document.getElementById('TimeModuleClockLine').style.msTransformOrigin="0 50%";
		document.getElementById('TimeModuleClockLine').style.msTransform="rotate(" + value + "deg)";
		document.getElementById('TimeModuleClockLine').style.transformOrigin="0 50%";
		document.getElementById('TimeModuleClockLine').style.transform="rotate(" + value + "deg)";
	}
	function AppendChooseBarDays()
	{
		var TDSize=($("#TimeModule").width()-20)*0.1;
		var TRSize,DivSize;
		if (TimeModuleStyleNow=="vertical")
		{
			TDSize=($("#TimeModule").width()-20)/7;
			TRSize=TDSize*1.1;	
			DIVSize=TDSize*0.9;	
		}
		else
		{
			TRSize=TDSize*0.85;	
			DIVSize=TRSize*0.8;
		}
		var Content='<div id="qa" style="padding:none;padding-left:10px;padding-right:10px">';//先限制高度
		Content+='<div><table style="font-weight:600;width:100%;background-color:inherit;text-align:center;padding:0px;border:none"><tr style="height:22px">';//开始借助table
		Content+='<td id="LastMonth" style="width:'+TDSize+'px;background-color:inherit;color:#424242;font-size:20px;cursor:pointer"><</td>';
		Content+='<td id="ChooseYearAndMonth"></td>';//显示当前所选择的年月
		Content+='<td id="NextMonth" style="width:'+TDSize+'px;background-color:inherit;color:#424242;font-size:20px;cursor:pointer">></td>';
		Content+='</tr></table></div>';
			//接下来就是日期部分了
		Content+='<table border="0" cellspacing="0" cellpadding="0" style="width:100%;background-color:inherit;text-align:center;padding:0px;border:none">';//留出一些空白
		Content+='<tr style="height:34px;color:#757575;">'//高度设置要谨慎
		for (i=0;i<7;++i)
		{
			Content=Content+'<td>'+DateDaysOfWeekInShort[(i+WeekBeginDay)%7]+'</td>';
		}
		Content+='</tr>';
		for (var l=0;l<Lines;++l)
		{
			Content+='<tr style="height:'+TRSize+'px">';//添加行
			for (var r=0;r<Rows;++r)
			{
				Content+='<td class="FillDays"><div class="OperateArea"style="margin:auto;cursor:pointer;position:relative;width:'+DIVSize+'px;height:'+DIVSize+'px;line-height:'+DIVSize+'px;"></div></td>';//class是为了方便选取
			}
			Content+='</tr>';
		}
		Content+='</table>';
		Content+='<div class="OperateArea" style="padding-bottom:'+TimeModuleBottomButtonBottomPadding+'px;height:+'+TimeModuleBottomButtonHeight+'px;font-size:16px;color:'+MainColor+'">';
		
		if (TimeModuleBottomButton)
		{
			if (TimeModuleStyleNow=="vertical")
			{
				FirstButtonLeft="50%";
				SecondButtonLeft="82%";	
			}
			else
			{
				FirstButtonLeft="60%";
				SecondButtonLeft="82%";	
			}
			Content+='<div id="CancelChooseTime" style="cursor:pointer;position:relative;width:50px;height:20px;left:'+FirstButtonLeft+';">'+BottomButton[0]+'</div>';
			Content+='<div id="ConfirmChooseTime" style="cursor:pointer;position:relative;width:50px;height:20px;left:'+SecondButtonLeft+';top:-20px">'+BottomButton[1]+'</div>';
		}
		Content+='</div></div></div>';	
		$("#TimeModuleChooseBar").html(Content);
		$("#NextMonth").click(NextMonth);
		$("#LastMonth").click(LastMonth);
		$("td.FillDays div").click(ChooseDay);
		$("#ConfirmChooseTime").click(ConfirmChooseTime);
		$("#CancelChooseTime").click(CancelChooseTime);	
	}
	function ConfirmChooseTime()
	{
		setTimeout(function (){ChooseBarDisplay="Hours";AppendDisplayBar();AppendChooseBar();},300);
	}
	function FillInputElement()
	{
		var InputTime=DateYear+"/"+DateMonth+"/"+DateDay+" "+DateHour+":"+DateMinute;
		InputElement.val(InputTime);
	}
	function CancelChooseTime()
	{
		 HideTimeModule();	
	}
	function NextMonth()
	{
		RemoveTimeModule();//除去当前
		DateMonth++;//月份加一
		if (DateMonth>12)//修改月份和年数
		{
			DateYear++;
			DateMonth=DateMonth-12;	
		}
		SetDateNow();
		ShowTimeModule();
	}
	function SetDateNow()
	{
		DateNow.setFullYear(DateYear);
		DateNow.setMonth(DateMonth-1);
		DateNow.setDate(DateDay);
		DateDayOfWeek=DateNow.getDay();
		DateNow.setHours(DateHour,DateMinute,0,0);	
	}
	function LastMonth()
	{
		RemoveTimeModule();//除去当前
		DateMonth--;//月份加一
		if (DateMonth<1)//修改月份和年数
		{
			DateYear--;
			DateMonth=DateMonth+12;	
		}
		SetDateNow();
		ShowTimeModule();
	}
	function ChooseDay()//选中日期后改变上方栏中日期并给该日期添加颜色
	{
		var Day=$(this).html();
		if (Day=="")
			return;
		else
		{
			DateDay=Day;//改变当前被选中的天数，然后调用状态栏调整函数
			SetDateNow();
			DisplayTimeBarAdjust();
			HighlightChooseFillDay();//着色	
			if (TimeModuleAutoJump!=false)
				setTimeout(function (){ChooseBarDisplay="Hours";AppendDisplayBar();AppendChooseBar();},300);
		}
	}
	function DisplayTimeBarAdjust()//调整状态栏的信息
	{
		var Day=TimeModuleMonthsInShort[DateMonth-1]+' '+DateDay;
		$("#DisplayTimeBarDay").html(Day);
		$("#DisplayTimeBarDayOfWeek").html(DateDaysOfWeek[DateDayOfWeek]+",");	
	}
	function RemoveTimeModule()//移除整个模块
	{
		$("#TimeModuleBackground").remove();
		$("#TimeModule").remove();	
	}
	function HideTimeModule()
	{
		$("#TimeModuleBackground").remove();
		$("#TimeModule").fadeOut(100);	
	}
	function ShowTimeModule()//显示时间模块并计算调整
	{
		CalculateParameter();
		AppendTimeModule();
		FillTimeModule();	
		HighlightChooseFillDay();
		AdjustPlace();
		if (IfFirstAppear)//若第一次出现
			$("#TimeModule").fadeIn(600);
		else
			$("#TimeModule").css({"display":"block"});
		IfFirstAppear=false;
	}
	function HighlightChooseFillDay()//突出显示被选择中的天
	{
		var Size=$("td.FillDays div").size();
		for (var i=0;i<Size;++i)
		{
			var FillDay="td.FillDays:eq("+i+") div";
			if ($(FillDay).html()=="")
				;
			else if ($(FillDay).html()==DateDay)
			{
				//计算得到border-radius
				var TDWidth=$(FillDay).width();
				var TDHeight=$(FillDay).height();
				var BorderRadius=TDWidth;
				if (BorderRadius>TDHeight)
					BorderRadius=TDHeight;
				BorderRadius=BorderRadius/2;
				$(FillDay).css({"color":"white","border-radius":BorderRadius,"background-color":MainColor});	
			}
			else
			{
				$(FillDay).css({"border":"none","background-color":BackgroundColor,"color":Color});
			}
		}
	}
	function AdjustPlace()//调整位置
	{
		var WindowWidth=$(window).width();
		var WindowHeight=$(window).height();
		if (WindowWidth<WindowHeight)
		{
			if ((TimeModuleLeft==null)&&(TimeModuleTop==null))
			{
				TimeModuleLeft=(WindowWidth-$("#TimeModule").width())/2;	
				TimeModuleTop=(WindowHeight-$("#TimeModule").height())/2;
			}
		}
		if ((InputElement.is("input"))&&(TimeModulePosition=="absolute"))
		{
			if (TimeModuleTop==null)
				TimeModuleTop=Number(InputElement.offset().top+InputElement.height()+10)+"px";	
			if (TimeModuleLeft==null)
				TimeModuleLeft=Number(InputElement.offset().left)+"px";
		}
		if (TimeModuleTop==null)
				TimeModuleTop=="0px";	
		if (TimeModuleLeft==null)
			TimeModuleLeft="0px";
		var WindowWidth=$(window).width();
		var WindowHeight=$(window).height();
		$("#TimeModule").css({"top":TimeModuleTop,"left":TimeModuleLeft});
		return;
	}
	function AppendBackground()
	{
		var Width=document.body.offsetWidth;
		var Height=document.body.offsetHeight;
		if (Width<$(window).width())	
			Width=$(window).width();
		if (Height<$(window).height())
			Height=$(window).height();
		var Background='<div id="TimeModuleBackground" style="position:fixed;top:0px;left:0px;width:'+Width+'px;height:'+Height+'px;z-index:100;background-color:#ccc;opacity:0.6"></div>';
		$(document.body).append(Background);
	}
	function AdjustWidth()
	{
		if ((TimeModuleStyle=="auto")&&(TimeModuleStyleNow=="vertical"))
		{
			$("#TimeModule").width(Number(TimeModuleWidth.replace("px",""))*0.6);
			var WindowWidth=$(window).width();
			var WindowHeight=$(window).height();
			if (WindowWidth<WindowHeight)
			{
				var Width=WindowWidth*0.6;
				$("#TimeModule").width(Width);	
			}
		}
	}
}